﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchEdu.Framework.Logging;
using SchEdu.Framework.Validators;
using SchEdu.Framework.DataModel;
using System.Windows.Forms.DataVisualization.Charting;

namespace SchEdu.Framework.Win
{
    public partial class MachineUtilizationPercentageChart : UserControl
    {
        private Logger logger = new Logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString());

        protected Logger Logger
        {
            get { return logger; }
            set { logger = value; }
        }

        public MachineUtilizationPercentageChart()
        {
            InitializeComponent();
        }

        public void DisplayChart(SchEduData schEduData, SchEduDataSet.SolutionRow solution)
        {
            Validate(schEduData, solution);
            AllocationList allocations = new AllocationList(solution.GetAllocationRows());
            Dictionary<System.Guid, string> machines = new Dictionary<Guid, string>();
            foreach (SchEduDataSet.AllocationRow row in allocations)
            {
                if (machines.ContainsKey(row.MachineGuid) == true)
                {
                    continue;
                }

                machines.Add(row.MachineGuid, row.Machine);
            }

            int makeSpan = allocations.GetMakespan();
            int hundredPercentage = makeSpan + 1;

            utilizationChart.Series[0].Points.Clear();

            foreach (KeyValuePair<System.Guid, string> machine in machines)
            {
                int utilization = 0;
                foreach (SchEduDataSet.AllocationRow allocation in allocations)
                {
                    if (machine.Key == allocation.MachineGuid)
                    {
                        utilization += allocation.ProcessingTime;
                    }
                }
                int pointIndex = utilizationChart.Series[0].Points.AddXY(machine.Value, Math.Round((double)utilization / hundredPercentage * 100, 2));
                utilizationChart.Series[0].Points[pointIndex].IsValueShownAsLabel = true;
            }

        }

        private void Validate(SchEduData schEduData, SchEduDataSet.SolutionRow solution)
        {
        }

    }
}
